package com.tbit.uqbike.webmanager.controller.interceptor;

import com.tbit.common.utils.RpcSecurityUtil;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.TokenService;
import com.tbit.uqbike.service.business.AccountUserFreeLoginService;
import com.tbit.uqbike.service.business.AccountUserService;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 *
 * @author Leon
 * 2014-4-10 上午10:22:54
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountUserDao accountUserDao;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private AccountUserFreeLoginService freeLoginService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (RpcSecurityUtil.authentication(request)) return true;
        String token = null;
        token = request.getHeader(HttpHeadName.REQUEST_ATTR_NAME_TOKEN);
        if (null == token) {
            token = request.getParameter(HttpHeadName.REQUEST_ATTR_NAME_TOKEN);
        }
        if (StringUtils.isEmpty(token)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        LoginInfo info = tokenService.getLoginInfo(token);
        request.setAttribute(HttpHeadName.REQUEST_ATTR_NAME_USER, info);
        if (info == null || StringUtils.isEmpty(info.getRoles())) {
            throw new BaseException(SystemCodeEnum.TOKEN_EXPIRE);
        }
//        /**获取登录账号的信息*/
//        List<AccountUserBound> accountUserBounds = accountUserDao.getByAccountUserId(info.accountUserId);
//        if (!accountUserBounds.isEmpty()){
//            for (AccountUserBound accountUserBound : accountUserBounds) {
//                /**如果登录账号的过期时间不为空，且过期时间小于今天的时间*/
//                if (accountUserBound.getExpireDate() != null && DateTimeUtil.StringToDateTime(accountUserBound.getExpireDate() + " 23:59:59").getTime() <= System.currentTimeMillis()) {
//                    throw new BaseException(SystemCodeEnum.TOKEN_EXPIRE);
//                }
//            }
//        }

        //过期校验
        accountUserService.expireCheck(info.getAccountUserId());

        //账号在有效时间内定期免登录
        freeLoginService.freeLogin(info.getAccountUserId(), info.getId());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex) {
    }
}
